package com.example.music.adapter

import Utils.loadUrl
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_leancloud.model.Music
import com.example.music.databinding.MusicItemBinding

class MusicAdapter : RecyclerView.Adapter<MusicAdapter.MyViewHolder>() {
    private var musicList = listOf<Music>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int)
            : MyViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return MyViewHolder(MusicItemBinding.inflate(inflater))
    }

    override fun getItemCount(): Int {
        return musicList.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.binding(musicList[position])
    }

    class MyViewHolder(val binding: MusicItemBinding) : RecyclerView.ViewHolder(binding.root) {

        fun binding(model: Music) {
            binding.img.loadUrl(model.image)
            binding.songTitle.text = model.title
            binding.singers.text = model.singer
        }
    }

    fun setNewData(newData: List<Music>) {
        musicList = newData
        notifyDataSetChanged()
    }
}